package control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import Dao.WordDaoimpl;
import entity.Word;
import service.RecordService;
import service.RecordServiceimpl;
import service.WordService;
import service.WordServiceimpl;

public class GetMywords  extends HttpServlet{
	
	WordService ws =  new WordServiceimpl();
	RecordService rsc = new  RecordServiceimpl();
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("record update");
		new Thread() {
			public void run() {
				while(true) {
					
					try {
						rsc.delete();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					try {
						sleep(24*3600*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			};
		}.start();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String email = null;
		Integer typeid = null;;
		String password = null;
		for(Cookie c:req.getCookies()) {
			if(c.getName().equals("email")) {
				email = c.getValue();
			}
			if(c.getName().equals("typeid")) {
				typeid = Integer.valueOf(c.getValue());
			}
			if(c.getName().equals("password")) {
				password = c.getValue();
			}
		}
		req.setCharacterEncoding("utf-8");
		List<Word> l= null;
		try {
			l = ws.getmyword(email);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resp.setCharacterEncoding("utf-8");
		resp.getWriter().print(JSON.toJSONString(l));
	}
		
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}
